#!/usr/bin/python
###################################################################################################                                                                                              
#                                                                                              
# DEVELOPERS : Luca Sanna                                                                        
# VERSIONE : 0.1                                                                                  
#                                                                                                                    
# E-MAIL: sanluca78@gmail.com                                                                                        
# COPYRIGHT: (c) 2015                                                                      
# PROGRAMMA: Py-Home-Slave  
#This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.                                                                          
###################################################################################################
import time,sys,os
from threading import Thread
from baseconfig import *
from tools import *
from database import *
from ablib import Pin
#logCritical("")
class MyPulse(Thread):
    db=Mydata()
    #plcd=Myplcd()
    #pos=0
    #enter=False
    in1 = Pin('J4.11','INPUT')
    in2 = Pin('J4.13','INPUT')
    in3 = Pin('J4.15','INPUT')
    in4 = Pin('J4.17','INPUT')
    in5 = Pin('J4.19','INPUT')
    in6 = Pin('J4.21','INPUT')
    in7 = Pin('J4.23','INPUT')
    in8 = Pin('J4.25','INPUT')
    time.sleep(5)

    def actualTime(self):
        actualTime=time.localtime()
        self.year=actualTime[0]
        self.month=actualTime[1]
        self.day=actualTime[2]
        self.hour=actualTime[3]
        self.minute=actualTime[4]
    
    
       
    def status(self,id_status):
        x = self.db.view_configure()
        p=0
        for b in x:
            id=int(b[0])
            label=b[1]
            power=b[2]
            pulse=b[6]
            switch=b[7]
            button=b[8]

            if pulse==1:
                p=+1
                self.db.update_pulse('pulse%s' %id,p)
                
                if p==20:
                    self.db.update_pulse('pulse%s' %id,0)
                    p=0

       
    def run(self):

        while True:
            try:
                if self.in1.digitalRead()==0:
                    #logCritical("in 1 premuto")
                    self.status(1)
                   
                if self.in2.digitalRead()==0:
                    self.status(2)
                   
                if self.in3.digitalRead()==0:
                    self.status(3)
                   
                if self.in4.digitalRead()==0:
                    self.status(4)
                   
                if self.in5.digitalRead()==0:
                    self.status(5)
                   
                if self.in6.digitalRead()==0:
                    self.status(6)
                   
                if self.in7.digitalRead()==0:
                    self.status(7)
                   
                if self.in8.digitalRead()==0:
                    self.status(8)
                   
                   
            except Exception,e:
                logCritical("pulse while %s" %e)
            time.sleep(0.1)
